#!coding=utf-8
from django.http import *
from django.shortcuts import *

from django.db.models import Sum
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from enterprise_chemicals.models import *
from enterprise_basic_information.models import *

# 获取当前园区 和 企业列表
def isLogin(req):
	admin = req.session.get('admin', None)
	park = req.session.get('park', None)
	
	if not admin and not park:
		return False, False
	else:
		return admin, park

def material(request):
	try:
		admin, park = isLogin(request)

		if not admin and not park:
			return HttpResponse(u'当前用户信息错误')

		fills = Enter_fill.objects.filter(park_id = park.id).order_by('-create_at')

		fill_key = request.GET.get('fill', None)
		is_danger = request.GET.get('is_danger', None)
		is_emphasis = request.GET.get('is_emphasis', None)

		if fill_key:
			ms_query = Materials.objects.filter(app_status__fill_id__enter_fill__id=fill_key)
		else:
			fill_key = fills[0].id
			ms_query = Materials.objects.filter(app_status__fill_id__enter_fill__id=fills[0].id)

		if is_danger:
			if is_danger == '1':
				k = u'是'
			if is_danger == '0':
				k = u'否'

			ms_query = ms_query.filter(is_dangerous=k)
		else:
			is_danger = '' 

		if is_emphasis:
			if is_emphasis == '1':
				k = u'是'
			if is_emphasis == '0':
				k = u'否'

			ms_query = ms_query.filter(is_emphasis=k)
		else:
			is_emphasis = '' 

		ms_query.query.group_by = ['name']
		ms_query = ms_query.annotate(total_usage_amount=Sum('input_amount'))

		sum_query = ms_query.aggregate(result=Sum('input_amount'))

		page = request.GET.get('page')
		paginator = Paginator(ms_query, 10)

		result = paginator.page(page)
	except PageNotAnInteger:
		result = paginator.page(1)
	except EmptyPage:
		result = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse(u"非法请求\n\n"+ e.message)

	return render_to_response('park/data_detail/material_data_detail.html', { 'data':result, 'is_danger':is_danger, 'is_emphasis':is_emphasis, 'fill_key':fill_key, 'fills':fills, 'sum':sum_query, 'paginator':paginator }, context_instance=RequestContext(request))

def product(request):
	try:

		admin, park = isLogin(request)

		if not admin and not park:
			return HttpResponse(u'当前用户信息错误')

		fills = Enter_fill.objects.filter(park_id = park.id).order_by('-create_at')

		fill_key = request.GET.get('fill', None)
		is_danger = request.GET.get('is_danger', None)
		is_emphasis = request.GET.get('is_emphasis', None)

		if fill_key:
			ps_query = Product.objects.filter(app_status__fill_id__enter_fill__id=fill_key)
		else:
			fill_key = fills[0].id
			ps_query = Product.objects.filter(app_status__fill_id__enter_fill__id=fills[0].id)

		if is_danger:
			if is_danger == '1':
				k = u'是'
			if is_danger == '0':
				k = u'否'

			ps_query = ps_query.filter(is_dangerous=k)
		else:
			is_danger = ''

		if is_emphasis:
			if is_emphasis == '1':
				k = u'是'
			if is_emphasis == '0':
				k = u'否'

			ps_query = ps_query.filter(is_emphasis=k)
		else:
			is_emphasis = '' 

		ps_query.query.group_by = ['name']
		ps_query = ps_query.annotate(total_usage_amount=Sum('year_amount'))

		sum_query = ps_query.aggregate(result=Sum('year_amount'))

		page = request.GET.get('page')
		paginator = Paginator(ps_query, 10)

		result = paginator.page(page)
	except PageNotAnInteger:
		result = paginator.page(1)
	except EmptyPage:
		result = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)

	return render_to_response('park/data_detail/product_data_detail.html', { 'data':result, 'is_danger':is_danger, 'is_emphasis':is_emphasis, 'fill_key':fill_key, 'fills':fills, 'sum':sum_query, 'paginator':paginator }, context_instance=RequestContext(request))

def imp(request):
	try:
		admin, park = isLogin(request)

		if not admin and not park:
			return HttpResponse(u'当前用户信息错误')

		fills = Enter_fill.objects.filter(park_id = park.id).order_by('-create_at')

		fill_key = request.GET.get('fill', None)
		is_danger = request.GET.get('is_danger', None)
		is_emphasis = request.GET.get('is_emphasis', None)

		if fill_key:
			imps_query = InterMeditatesPro.objects.filter(app_status__fill_id__enter_fill__id=fill_key)
		else:
			fill_key = fills[0].id
			imps_query = InterMeditatesPro.objects.filter(app_status__fill_id__enter_fill__id=fills[0].id)

		if is_danger:
			if is_danger == '1':
				k = u'是'
			if is_danger == '0':
				k = u'否'

			imps_query = imps_query.filter(is_dangerous=k)
		else:
			is_danger = ''
		
		if is_emphasis:
			if is_emphasis == '1':
				k = u'是'
			if is_emphasis == '0':
				k = u'否'

			imps_query = imps_query.filter(is_emphasis=k)
		else:
			is_emphasis = '' 

		imps_query.query.group_by = ['name']
		imps_query = imps_query.annotate(total_usage_amount=Sum('year_amount'))

		sum_query = imps_query.aggregate(result=Sum('year_amount'))

		page = request.GET.get('page')
		paginator = Paginator(imps_query, 10)

		result = paginator.page(page)
	except PageNotAnInteger:
		result = paginator.page(1)
	except EmptyPage:
		result = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)

	return render_to_response('park/data_detail/imp_data_detail.html', { 'data':result, 'is_danger':is_danger, 'is_emphasis':is_emphasis, 'fill_key':fill_key, 'fills':fills, 'sum':sum_query, 'paginator':paginator }, context_instance=RequestContext(request))

def ids(request):
	try:
		admin, park = isLogin(request)

		if not admin and not park:
			return HttpResponse(u'当前用户信息错误')

		fills = Enter_fill.objects.filter(park_id = park.id).order_by('-create_at')

		fill_key = request.GET.get('fill', None)
		is_danger = request.GET.get('is_danger', '')
		is_emphasis = request.GET.get('is_emphasis', '')

		if not fill_key:
			fill_key = fills[0].id

		if is_danger:
			if is_danger == '1':
				is_danger_str = u" = '是'"
			if is_danger == '0':
				is_danger_str = u" = '否'"
		else:
			is_danger_str = " != ''"

		if is_emphasis:
			if is_emphasis == '1':
				is_emphasis_str = u" = '是'"
			if is_emphasis == '0':
				is_emphasis_str = u" = '否'"
		else:
			is_emphasis_str = " != ''"


		from django.db import connection,transaction
		cursor = connection.cursor()

		cursor.execute('''select 
							b.cr_type_id,c.name,sum(a.e_shechun) 
						from 
							enterprise_chemicals_ingredients as a, 
							enterprise_chemicals_chemicalsrelative as b, 
							enterprise_chemicals_ingredientstype as c 
						where 
							a.id = b.igds_entity_id and 
							b.cr_type_id = c.id and
							a.e_is_emphasis %s and
							a.e_is_dangerous %s and
							exists (
								select a.id from enterprise_basic_information_application_status a
								inner join enterprise_basic_information_fill b
								on a.fill_id = b.id
								where app_status_id = a.id 
								and enter_fill_id = %s
							)
						group by b.cr_type_id''' % (is_danger_str, is_emphasis_str, fill_key))

		reuslt_sum = cursor.fetchall()

		cursor.execute('''select
							b.id, b.e_name, b.e_code, b.e_is_dangerous, b.e_is_emphasis, c.name, sum(b.e_shechun) 
						from  
							enterprise_chemicals_chemicalsrelative as a, 
							enterprise_chemicals_ingredients as b, 
							enterprise_chemicals_ingredientstype as c 
						where 
							a.igds_entity_id = b.id and 
							a.cr_type_id = c.id and
							b.e_is_emphasis %s and
							b.e_is_dangerous %s and
							exists (
								select a.id from enterprise_basic_information_application_status a
								inner join enterprise_basic_information_fill b
								on a.fill_id = b.id
								where app_status_id = a.id 
								and enter_fill_id = %s
							)
						group by b.e_name, a.cr_type_id order by c.id'''  % (is_danger_str, is_emphasis_str, fill_key))


		ids_query = cursor.fetchall()
		
		page = request.GET.get('page')
		paginator = Paginator(ids_query, 10)
		result = paginator.page(page)

	except PageNotAnInteger:
		result = paginator.page(1)
	except EmptyPage:
		result = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse(u'非法请求'+ e.__str__())

	return render_to_response('park/data_detail/ids_data_detail.html', { 'data':result, 'fills':fills, 'fill_key':fill_key, 'is_danger':is_danger, 'is_emphasis':is_emphasis, 'data_sum':reuslt_sum, 'paginator':paginator }, context_instance=RequestContext(request))
